package funcMap

import (
	"entity"
	"html/template"
	"reflect"
	"strings"
	"time"
)

func Eq(args ...interface{}) bool {
	if len(args) == 0 {
		return false
	}
	x := args[0]
	switch x := x.(type) {
	case string, int, int64, byte, float32, float64:
		for _, y := range args[1:] {
			if x == y {
				return true
			}
		}
		return false
	}

	for _, y := range args[1:] {
		if reflect.DeepEqual(x, y) {
			return true
		}
	}
	return false
}

func Cut(str string) string {

	return str[0:10]
}
func GetDomain(str string) string {
	d := strings.Replace(str, ".qirina.com", "", 1)
	arr := strings.Split(d, ".")

	domain := ""

	for i := len(arr) - 1; i >= 0; i-- {
		domain = domain + "." + arr[i]
	}

	return strings.Replace(domain, ".", "", 1)
}
func TrimHtmlSafe(text string) template.HTML {
	text = strings.Replace(text, "-", " ", -1)
	return template.HTML(text)
}

func HtmlSafe(text string) template.HTML {

	return template.HTML(text)

}

func TestFuncMap() string {
	return "Fuk you"
}

func GetYear(Date time.Time) int {
	return Date.Year()
}

func GetMonth(Date time.Time) int {
	return int(Date.Month())
}

func GetMonthName(Date time.Time) time.Month {
	return Date.Month()
}

func GetDay(Date time.Time) int {
	return Date.Day()
}

func GetCategoryNews(mixedCategory string) entity.Category {
	var c entity.Category
	arrStrCate := strings.Split(mixedCategory, ",")
	if len(arrStrCate) == 2 {
		c.Alias = arrStrCate[0]
		c.Title = arrStrCate[1]
	}
	return c
}

func GetTagNews(mixedTag string) entity.Tag {
	var c entity.Tag
	arrStr := strings.Split(mixedTag, ",")
	if len(arrStr) == 2 {
		c.Alias = arrStr[0]
		c.Title = arrStr[1]
	}
	return c
}

func GetCategoryNewsSearch(mixedCategory string) []entity.Category {
	var arr []entity.Category
	arrStrCate := strings.Split(mixedCategory, "/")
	for _, e := range arrStrCate {
		var c entity.Category
		strCate := strings.Split(e, ",")
		if len(strCate) == 2 {
			c.Alias = strings.TrimSpace(strCate[0])
			c.Title = strings.TrimSpace(strCate[1])
			arr = append(arr, c)
		}
	}
	return arr
}

func GetTagNewsSearch(mixedTag string) []entity.Tag {
	var arr []entity.Tag
	arrStr := strings.Split(mixedTag, "/")
	for _, e := range arrStr {
		var c entity.Tag
		str := strings.Split(e, ",")
		if len(str) == 2 {
			c.Alias = strings.TrimSpace(str[0])
			c.Title = strings.TrimSpace(str[1])
			arr = append(arr, c)
		}
	}
	return arr
}
